﻿//////////////////////////////////////////////////////////////////////////////
//
//  Copyright © 1998-2024 Glodon Company Limited.  All rights reserved.
//
//  Use of this software is subject to the terms of the Glodon license
//  agreement provided at the time of installation or download, or which
//  otherwise accompanies this software in either electronic or hard copy form.  
//
//////////////////////////////////////////////////////////////////////////////

// Reviewed
#pragma once

#include "GcmpGUiBaseInterface.h"
#include "IControlDefinition.h"

#include <string>

namespace gcmp
{
    class IControlDefinitionLibraryEvent;

    /// \brief 控件数据定义的容器接口
    ///
    /// 创建控件数据定义时不会自动添加到IControlDefinitionLibrary中
    class GCMP_GUI_BASE_INTERFACE_EXPORT IControlDefinitionLibrary
    {
    public:
        /// \brief 控件数据定义的容器对象
        static IControlDefinitionLibrary* Get();

        /// \brief 析构函数
        virtual ~IControlDefinitionLibrary();

        /// \brief 添加控件数据定义对象
        ///
        /// 添加library的控件定义需保证唯一， 不唯一添加会失败
        /// \param pControlDefinition    控件数据定义
        /// \return  true表示成功， 如果IControlDefinitionLibrary已包含与pControlDefinition同Id的控件定义,则返回false
        virtual bool AddControlDefinition(OwnerPtr<IControlDefinition> pControlDefinition) = 0;

        /// \brief 根据id得到控件数据定义对象
        ///
        /// \param id    控件数据定义的ID
        virtual IControlDefinition* GetControlDefinition(const std::wstring& id) const = 0;

        /// \brief 根据id删除控件数据定义对象
        ///
        /// 删除控件定义对象时，其绑定的控件也会相应的删除，慎重调用
        /// \param id    控件数据定义的ID
        /// \return  true表示成功， 如果IControlDefinitionLibrary不包含Id为id的控件定义,则返回false
        virtual bool DeleteControlDefinition(const std::wstring& id) = 0;

        /// \brief 得到IControlDefinitionLibraryEvent对象
        ///
        /// \return IControlDefinitionLibraryEvent对象
        virtual IControlDefinitionLibraryEvent* GetEvent() = 0;

        /// \brief 获取所有的IControlDefinition
        ///
        /// \return 返回所有的IControlDefinition 集合
        virtual const std::vector<const IControlDefinition *> GetAllIControlDefinition() const = 0;
    };

    /// \brief 工具类
    class GCMP_GUI_BASE_INTERFACE_EXPORT ControlDefinitionLibraryUtil
    {
    public:
        /// \brief 根据id得到控件数据定义对象
        ///
        /// T 控件数据定义类型
        /// \param id    控件数据定义的ID
        /// \return
        template<typename T>
        static T* GetControlDefinition(const std::wstring& id)
        {
            IControlDefinitionLibrary* pLibrary = IControlDefinitionLibrary::Get();
            return pLibrary ? dynamic_cast<T*>(pLibrary->GetControlDefinition(id)) : nullptr;
        }
    };



}   // end namespace gcmp

